gfxpoly: added additional save method
authorMatthias Kramm <kramm@quiss.org>
Wed, 7 Apr 2010 01:33:19 +0000 (18:33 -0700)
committerMatthias Kramm <kramm@quiss.org>
Wed, 7 Apr 2010 01:33:19 +0000 (18:33 -0700)
lib/gfxpoly/Makefile
lib/gfxpoly/poly.c
lib/gfxpoly/poly.h
lib/gfxpoly/test.c

index bbfa391..4e182f7 100644 (file)
@@ -1,7 +1,7 @@
 all: test stroke
 include ../../Makefile.common
 
-CC = gcc -O2 -g
+CC = gcc -O2 -g -pg
 #CC = gcc -O3
 
 ../libbase.a: ../q.c ../q.h ../mem.c ../mem.h
index 7eb6509..4459cbf 100644 (file)
@@ -250,6 +250,50 @@ void gfxpoly_save(gfxpoly_t*poly, const char*filename)
     fclose(fi);
 }
 
+void gfxpoly_save_arrows(gfxpoly_t*poly, const char*filename)
+{
+    FILE*fi = fopen(filename, "wb");
+    fprintf(fi, "%% gridsize %f\n", poly->gridsize);
+    fprintf(fi, "%% begin\n");
+    int t;
+    double l = 5.0 / poly->gridsize;
+    double g = poly->gridsize;
+    gfxpolystroke_t*stroke = poly->strokes;
+    for(;stroke;stroke=stroke->next) {
+       fprintf(fi, "%g setgray\n", 0);
+
+       int s = stroke->dir==DIR_UP?stroke->num_points-1:0;
+       int end = stroke->dir==DIR_UP?-1:stroke->num_points;
+       int dir = stroke->dir==DIR_UP?-1:1;
+
+       point_t p = stroke->points[s];
+       s+=dir;
+       point_t o = p;
+       fprintf(fi, "%f %f moveto\n", p.x * g, p.y * g);
+       for(;s!=end;s+=dir) {
+           p = stroke->points[s];
+           int lx = p.x - o.x;
+           int ly = p.y - o.y;
+           double d = sqrt(lx*lx+ly*ly);
+           if(!d) d=1;
+           else   d = l / d;
+           double d2 = d*1.5;
+           fprintf(fi, "%f %f lineto\n", (p.x - lx*d2) * g, (p.y - ly*d2) * g);
+           fprintf(fi, "%f %f lineto\n", (p.x - lx*d2 + (ly*d))*g,
+                                         (p.y - ly*d2 - (lx*d))*g);
+           fprintf(fi, "%f %f lineto\n", p.x * g, p.y * g);
+           fprintf(fi, "%f %f lineto\n", (p.x - lx*d2 - (ly*d))*g, 
+                                         (p.y - ly*d2 + (lx*d))*g);
+           fprintf(fi, "%f %f lineto\n", (p.x - lx*d2) * g, (p.y - ly*d2) * g);
+           fprintf(fi, "%f %f moveto\n", p.x * g, p.y * g);
+           o = p;
+       }
+       fprintf(fi, "stroke\n");
+    }
+    fprintf(fi, "showpage\n");
+    fclose(fi);
+}
+
 inline static event_t* event_new()
 {
     event_t*e = rfx_calloc(sizeof(event_t));
@@ -1154,18 +1198,17 @@ static void add_horizontals(gfxpoly_t*poly, windrule_t*windrule, windcontext_t*c
         actlist_verify(actlist, y-1);
 #endif
        edgestyle_t*fill = 0;
-       char dir_up = 0;
-       char dir_down = 0;
+       int dir_up = 0;
+       int dir_down = 0;
 
         do {
            assert(e->s1->fs);
             if(fill && x != e->p.x) {
-               assert(!dir_up || !dir_down);
-               assert(dir_up || dir_down);
 #ifdef DEBUG
                 fprintf(stderr, "%d) draw horizontal line from %d to %d\n", y, x, e->p.x);
 #endif
                assert(x<e->p.x);
+               assert(dir_up || dir_down);
 
                 gfxpolystroke_t*stroke = rfx_calloc(sizeof(gfxpolystroke_t));
                stroke->next = poly->strokes;
@@ -1173,7 +1216,12 @@ static void add_horizontals(gfxpoly_t*poly, windrule_t*windrule, windcontext_t*c
 
                stroke->num_points = 2;
                stroke->points = malloc(sizeof(point_t)*2);
-               stroke->dir = dir_up?DIR_UP:DIR_DOWN;
+               if(dir_up < 0 || dir_down > 0) {
+                   stroke->dir = DIR_UP;
+               } else {
+                   stroke->dir = DIR_DOWN;
+               }
+
                stroke->fs = fill;
                point_t a,b;
                 a.y = b.y = y;
@@ -1210,20 +1258,14 @@ static void add_horizontals(gfxpoly_t*poly, windrule_t*windrule, windcontext_t*c
                     e->s2 = 0;
                     hqueue_put(&hqueue, e);
                     left = actlist_left(actlist, s);
-                   if(e->s1->dir==DIR_UP)
-                       dir_up^=1;
-                   else
-                       dir_down^=1;
+                   dir_down += e->s1->dir==DIR_UP?1:-1;
                     break;
                 }
                 case EVENT_END: {
                     left = actlist_left(actlist, s);
                     actlist_delete(actlist, s);
                    advance_stroke(0, &hqueue, s->stroke, s->polygon_nr, s->stroke_pos);
-                   if(e->s1->dir==DIR_DOWN)
-                       dir_up^=1;
-                   else
-                       dir_down^=1;
+                   dir_up += e->s1->dir==DIR_UP?1:-1;
                     break;
                 }
                 default: assert(0);
index 4154e79..180a4f1 100644 (file)
@@ -98,6 +98,7 @@ int gfxpoly_num_segments(gfxpoly_t*poly);
 int gfxpoly_size(gfxpoly_t*poly);
 void gfxpoly_dump(gfxpoly_t*poly);
 void gfxpoly_save(gfxpoly_t*poly, const char*filename);
+void gfxpoly_save_arrows(gfxpoly_t*poly, const char*filename);
 gfxpoly_t* gfxpoly_process(gfxpoly_t*poly1, gfxpoly_t*poly2, windrule_t*windrule, windcontext_t*context);
 
 gfxpoly_t* gfxpoly_intersect(gfxpoly_t*p1, gfxpoly_t*p2);
index e968c17..509f3ee 100644 (file)
@@ -211,22 +211,28 @@ int test1(int argn, char*argv[])
 {
     gfxline_t*box1 = gfxline_makerectangle(50,50,150,150);
     gfxline_t*box2 = gfxline_makerectangle(100,100,200,200);
-    gfxline_t*box3 = gfxline_makerectangle(100,100,200,200);
+    gfxline_t*box3 = gfxline_makerectangle(200,100,300,200);
+    gfxline_t*box4 = gfxline_makerectangle(300,200,400,400);
+    gfxline_t* board = mkchessboard();
     gfxline_t*star = mkstar(50,50, 150,150);
     gfxline_t*b = 0;
     b = gfxline_append(b, box1);
     b = gfxline_append(b, box2);
-    //b = gfxline_append(b, box3);
+    b = gfxline_append(b, box3);
+    b = gfxline_append(b, box4);
 
     gfxmatrix_t matrix;
     memset(&matrix, 0, sizeof(gfxmatrix_t));
-    double ua=0.1;
-    matrix.m00=cos(ua);matrix.m10=sin(ua);
-    matrix.m01=-sin(ua);matrix.m11=cos(ua);
+    matrix.m00 = 1.0;
+    matrix.m11 = 1.0;
+    matrix.tx = 200;
+    matrix.ty = 200;
+    gfxline_transform(board, &matrix);
+    b = gfxline_append(b, board);
 
     //gfxline_transform(b, &matrix);
 
-    gfxline_dump(b, stderr, "");
+    //gfxline_dump(b, stderr, "");
     gfxpoly_t*poly = gfxpoly_from_fill(b, 0.05);
     
     gfxline_free(box1);
@@ -234,9 +240,10 @@ int test1(int argn, char*argv[])
     gfxline_free(box3);
     gfxline_free(star);
 
-    gfxpoly_dump(poly);
+    //gfxpoly_dump(poly);
     gfxpoly_t*poly2 = gfxpoly_process(poly, 0, &windrule_evenodd, &onepolygon);
-    gfxpoly_dump(poly2);
+    //gfxpoly_dump(poly2);
+    gfxpoly_save_arrows(poly2, "test.ps");
     gfxpoly_destroy(poly);
     gfxpoly_destroy(poly2);
 }
@@ -639,6 +646,6 @@ void test5(int argn, char*argv[])
 
 int main(int argn, char*argv[])
 {
-    test1(argn, argv);
+    test4(argn, argv);
 }